package service

import (
	"errors"
	"ginvueadmin/global"
	"ginvueadmin/models"
	"time"

	"gorm.io/gorm"
)

// 获取 redis jwt
func GetRedisJwt(userName string) (redisJwt string, err error) {
	redisJwt, err = global.REDIS.Get(userName).Result()

	return redisJwt, err
}

// 设置 reids jwt
//
// 过期时间为 7 天：60 * 60 * 24 * 7 * time.Second
//
// @params {string} token
// @params {string} key userName
func SetRedisJwt(token string, userName string) (err error) {
	t := 60 * 60 * 24 * 7 * time.Second
	err = global.REDIS.Set(userName, token, t).Err()

	return err
}

// 创建 jwt black list
//
// @param {models.JwtBlackList} blackJwt
func JsonInBlacklist(blackJwt models.JwtBlacklist) (err error) {
	err = global.DB.Create(&blackJwt).Error
	return err
}

// ----------

// 判断当前登录用户 token 是否在 jwt_black_list 表中
func IsBlackList(jwt string) bool {
	isNotFound := errors.Is(global.DB.Where("jwt = ?", jwt).First(&models.JwtBlacklist{}).Error, gorm.ErrRecordNotFound)

	return !isNotFound
}
